package com.lucy.flow.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.lucy.common.domain.ResponseDTO;
import com.lucy.flow.constant.AssignTypeConst;
import com.lucy.flow.domain.entity.FlowNodeAssign;
import com.lucy.flow.domain.entity.FlowWork;
import com.lucy.flow.event.FlowWorkDeletedEvent;
import com.lucy.flow.loader.TaskUserLoader;
import com.lucy.flow.params.FlowWorkQueryParams;
import com.lucy.flow.provider.TaskUserProvider;
import com.lucy.flow.service.FlowWorkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("flow")
@Api(tags = "流程实例管理")
public class FlowWorkController {
    @Autowired
    FlowWorkService flowWorkService;

    @Autowired
    TaskUserLoader taskUserLoader;

    /**
     * 应用上下文注册
     */
    @Resource
    ApplicationContext context;

    @DeleteMapping("work/{id}")
    @ApiOperation(value = "流程实例删除",notes = "")
    public ResponseDTO<Object> flowWorkDelete(@PathVariable("id") String id){
        FlowWork flowWork = flowWorkService.getOneUnwrap(id);
        flowWorkService.deleteOne(id);
        // 事件发布
        context.publishEvent(new FlowWorkDeletedEvent(this,flowWork));
        return ResponseDTO.succMsg("删除成功");
    }

    @GetMapping("work")
    @ApiOperation(value = "流程实例列表",notes = "")
    public ResponseDTO<Object> flowWorkListQuery(FlowWorkQueryParams params){
        Sort sort;
        if(params.getOrder().equalsIgnoreCase("DESC")){
            sort = Sort.by(Sort.Direction.DESC, params.getSortBy());
        }else {
            sort = Sort.by(Sort.Direction.ASC, params.getSortBy());
        }

        // tips : 页码对应 - 1
        Pageable pageable = PageRequest.of(params.getPageNum() - 1 , params.getPageSize(), sort);

        FlowWork flowWork = new FlowWork();
        BeanUtil.copyProperties(params,flowWork, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        Example<FlowWork> example = Example.of(flowWork);

        return ResponseDTO.succData(flowWorkService.getAll(pageable,example));
    }

    @GetMapping("work/{id}")
    @ApiOperation(value = "流程实例查询",notes = "")
    public ResponseDTO<Object> flowWorkQuery(@PathVariable("id") String id){
        return ResponseDTO.succData(
                flowWorkService.getOneUnwrap(id)
        );
    }

    // 可以根据 assign 查询 待审批的流程列表
//    @GetMapping("work/load")
//    @ApiOperation(value = "测试加载")
//    public ResponseDTO<Object> testLoad(){
//        CompletableFuture<List<Long>> future1 = taskUserLoader.load(AssignTypeConst.DEPT,"1");
//        CompletableFuture<List<Long>> future2 = taskUserLoader.load(AssignTypeConst.ROLE,"1");
//        CompletableFuture<List<Long>> future3 = taskUserLoader.load(AssignTypeConst.USER,"1");
//        CompletableFuture<Void> allFutures = CompletableFuture
//                .allOf (future1,future2,future3);
//        Set<Long> idSet = new HashSet<>();
//        idSet.addAll(future1.join());
//        idSet.addAll(future2.join());
//        idSet.addAll(future3.join());
//        return ResponseDTO.succData(
//                new ArrayList<>(idSet)
//        );
//
//    }

}
